
$(document).ready(function () {

    /* base
    ----------------------------------------------------------*/
    $('a').focus(function () {
        $(this).blur();
    });
    
    /* menu click
    ----------------------------------------------------------*/
    $(".stitle").click(function () {
    	$("ul.slevel").hide();
    	$(this).next().fadeIn(300);
    });
    
    /* rights
    ----------------------------------------------------------*/
	$(".right").each(function(){
		var rightId = $(this).attr("rightid");
		for (var i=0; i<V_User_Rights.length; i++ ){
			if (rightId == V_User_Rights[i]){
				$(this).removeClass("right");
				continue;
			}
		}
    });

  


});


/* global function
----------------------------------------------------------*/
$.extend({
	
	selMt: function (mtThis, v_this, thisid, parentid){
		mtThis.html("");
		mtThis.append("<option value='0'>" + Const_Global_DefaultOption + "</option>");
		for (entity in v_this) {
			if(parentid == 0 || v_this[entity]["parent"]== parentid){
					mtThis.append("<option value='" + entity + "'>" + v_this[entity]["name"] + "</option>");
			}
		}
		mtThis.val(thisid);
	},
	
	initMtSel: function (mtF, mtS, mtT, hmt){  
	    mtF.append("<option value='0'>" + Const_Global_DefaultOption + "</option>");
		for (entityF in V_MT_F) {
			mtF.append("<option value='" + entityF + "'>" + V_MT_F[entityF]["name"] + "</option>");
		}
		mtS.append("<option value='0'>" + Const_Global_DefaultOption + "</option>");
		mtT.append("<option value='0'>" + Const_Global_DefaultOption + "</option>");
		var selid = hmt.val();
		if(hmt.val() != 0){
			var testSel = false;
			for (entityT in V_MT_T) {
				if(entityT == hmt.val()){
					$.selMt(mtT,V_MT_T,entityT,V_MT_T[entityT]["parent"]);
					for(entityS in V_MT_S){
						if(entityS == V_MT_T[entityT]["parent"]){
							$.selMt(mtS,V_MT_S,entityS,V_MT_S[entityS]["parent"]);
							break;
						}
					}
					$.selMt(mtF,V_MT_F,V_MT_S[entityS]["parent"],0);
					testSel=true;
					break;
				}
			}
			if(testSel!=true){
				
				for(entityS in V_MT_S){
					if(entityS == hmt.val()){
						$.selMt(mtS,V_MT_S,entityS,V_MT_S[entityS]["parent"]);
						$.selMt(mtF,V_MT_F,V_MT_S[entityS]["parent"],0);
						testSel=true;
						break;
					}
				}
			}
			if(testSel!=true){
				mtF.val(hmt.val());
			}
		}

		mtF.bind('change', function() {
			mtS.html("");
			mtS.append("<option value='0'>" + Const_Global_DefaultOption + "</option>");
			mtT.html("");
			mtT.append("<option value='0'>" + Const_Global_DefaultOption + "</option>");
			hmt.val(mtF.val());
			if (mtF.val() == 0)	return;
			if (V_MT_F[$(this).val()]["children"] != null){
				for (var i=0; i<V_MT_F[$(this).val()]["children"].length; i++ ){
					var entityS = V_MT_F[$(this).val()]["children"][i];
					mtS.append("<option value='" + entityS + "'>" + V_MT_S[entityS]["name"] + "</option>");
				}		
			}
		});		
		mtS.bind('change', function() {
			mtT.html("");
			mtT.append("<option value='0'>" + Const_Global_DefaultOption + "</option>");
			if (mtS.val() != 0)	
				hmt.val(mtS.val());
			else {
				hmt.val(mtF.val());
				return;
			}				
			if (V_MT_S[$(this).val()]["children"] != null){
				for (var i=0; i<V_MT_S[$(this).val()]["children"].length; i++ ){
					var entityT = V_MT_S[$(this).val()]["children"][i];
					mtT.append("<option value='" + entityT + "'>" + V_MT_T[entityT]["name"] + "</option>");
				}		
			}
			hmt.val(mtS.val());
		});
		mtT.bind('change', function() {
			if (mtT.val() != 0)	
				hmt.val(mtT.val());
			else 
				hmt.val(mtS.val());
		});
	},
	initDialogUser: function (btnOpen, inputUserId, inputUserName, type, modal){  
		btnOpen.bind('click', function() {
	    	$( "#dialog_user" ).dialog({
	    		modal: true,
	    		width: 500,
	    		minHeight: 360,
	    		buttons: {
	    			"确 定": function() {
	    				var objChecked = $('input:radio[name=dialog_user]:checked');
	    				inputUserId.val(objChecked.val());
	    				inputUserName.val(objChecked.parents("tr").children(".display_name").text());
	    				$( this ).dialog( "close" );
	    			},
	    			"取 消": function() {
	    				$( this ).dialog( "close" );
	    			}
	    		}
	    	});
		});
	},
	initDialogCompany: function (btnOpen, inputUserId, inputUserName, type, modal){  
		btnOpen.bind('click', function() {
	    	$( "#dialog_company" ).dialog({
	    		modal: true,
	    		width: 500,
	    		minHeight: 360,
	    		buttons: {
	    			"确 定": function() {
	    				var objChecked = $('input:radio[name=dialog_company]:checked');
	    				inputUserId.val(objChecked.val());
	    				inputUserName.val(objChecked.parents("tr").children(".company_name").text());
	    				$( this ).dialog( "close" );
	    			},
	    			"取 消": function() {
	    				$( this ).dialog( "close" );
	    			}
	    		}
	    	});
		});
	}	
});



/* initial material type
----------------------------------------------------------*/
jQuery.fn.initMtValue = function(){
	
	var typeId = $(this).text();
	
	if (V_MT_F[typeId]!=undefined){
		$(this).text(V_MT_F[typeId]["name"]);
	} else if (V_MT_S[typeId]!=undefined){
		$(this).text(V_MT_S[typeId]["name"]);
	} else if (V_MT_T[typeId]!=undefined){
		$(this).text(V_MT_T[typeId]["name"]);
	} else {
		$(this).text(Const_Global_Unknown);
	}

};

/* Array
----------------------------------------------------------*/
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
};

function glInitOption(sel, list, defaultValue) {
    var eleOption;
    var optHtml;

    eleOption = document.createElement("option");
    eleOption.value = 0;
    eleOption.innerHTML = '---请选择---';
    sel.appendChild(eleOption);

    for (var i in list) {
        try {
            eleOption = document.createElement("option");
            eleOption.value = i;
            eleOption.innerHTML = list[i];
            if (i == defaultValue)
                eleOption.selected = true;
            else
                eleOption.selected = false;
            sel.appendChild(eleOption);
        }
        catch (err) {
        }
    }
}